package org.petstore.web.servlet;

import org.petstore.domain.Item;
import org.petstore.domain.Product;
import org.petstore.service.CatalogService;
import org.petstore.service.impl.CatalogServiceImpl;
import org.petstore.web.servlet.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


public class ViewIProductServlet extends HttpServlet {
    private static final String VIEW_PRODUCT = "/WEB-INF/jsp/catalog/Product.jsp";

    private final CatalogService catalogService = MyUtil.getApplicationContext().getBean("catalogService", CatalogService.class);


    private String productId;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        productId = request.getParameter("productId");
        Product product = catalogService.getProduct(productId);

        //todo
        List<Item> ItemList = catalogService.getItemListByProduct(productId);

        //itemId =request.getParameter("itemId");
        // ItemDAO itemdao =new ItemDAOImpl();
        // Item item=itemdao.getItem(itemId);


        HttpSession session = request.getSession();

        session.setAttribute("product", product);
        session.setAttribute("itemList", ItemList);

        request.getRequestDispatcher(VIEW_PRODUCT).forward(request, response);
    }
}
